package com.itheima.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import com.mysql.fabric.xmlrpc.base.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
//    私有
    private  StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置请求和响应编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

//        1.获取方法名
        String method = req.getParameter("method");
        if ("selectByPage".equals(method)) {
//            分页查询功能
            selectByPage(req, resp);
        }else if("addStu".equals(method)){
            addStu(req, resp);
        }else if("updateStu".equals(method)){
            updateStu(req,resp);
        }else if("deleteStu".equals(method)){
            delete(req, resp);
        }
    }



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /*分页查询功能*/
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
//        获取请求参数
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

//        调用业务层的查询方法
        Page page = studentService.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
//        封装PageInfo
        PageInfo pageInfo = new PageInfo(page);
//        将info转换为json，响应给客户端
        try {
            String json = new ObjectMapper().writeValueAsString(pageInfo);
            resp.getWriter().write(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //添加数据功能
    private void addStu(HttpServletRequest req, HttpServletResponse resp) {
        //获取请求参数
        Map<String, String[]> map = req.getParameterMap();
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        // 封装Student对象
        Student stu = new Student();

        //注册日期转换器方法
        dataConvert();

        try {
            BeanUtils.populate(stu,map);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        调用业务员的添加方法
        studentService.addStu(stu);

//        重定向到分页查询功能
        try {
            resp.sendRedirect(req.getContextPath() + "/studentServlet?method=selectByPage&currentPage=" + currentPage + "&pageSize=" + pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*日期转换*/
    private void dataConvert() {
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                return null;
            }
        }, Data.class);
    }

    /*修改数据功能*/
    private void updateStu(HttpServletRequest req, HttpServletResponse resp) {
            //获取请求参数
        Map<String, String[]> map = req.getParameterMap();
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        //封装Student
        Student student = new Student();

        //注册日期转换器方法
        dataConvert();

        try {
            BeanUtils.populate(student,map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //调用业务层方法
        studentService.updateStu(student);

        //重定向到分页查询功能

        try {
            resp.sendRedirect(req.getContextPath() + "/studentServlet?method=selectByPage&currentPage=" + currentPage + "&pageSize=" + pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*删除学生功能*/
    private void delete(HttpServletRequest req, HttpServletResponse resp) {
//        获取请求参数
        String number = req.getParameter("number");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

//        调用业务层方法
        studentService.deleteStu(number);

//        重定向到分页查询功能
        try {
            resp.sendRedirect(req.getContextPath() + "/studentServlet?method=selectByPage&currentPage=" + currentPage + "&pageSize=" + pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }}
}
